
package test5.lupeng.jf.com.teamdemo.activitys.found;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.activitys.SendDonTaiActivity;
import test5.lupeng.jf.com.teamdemo.adapter.ImageAdapter;
import test5.lupeng.jf.com.teamdemo.adapter.NewMessageAdapter;
import test5.lupeng.jf.com.teamdemo.bean.RequestLast;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseLast;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

//动态广场
public class FengxiangActivity extends AppCompatActivity {

    private static final String TAG = "FengxiangActivity";
    private RecyclerView dtVp;
    private RecyclerView lastVp;
    private String userId;
    ArrayList<ResponseLast.ResultBean> results;
    private NewMessageAdapter adapter;
    private ImageAdapter imageAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fengxiang);
        lastVp = (RecyclerView) findViewById(R.id.lastVp);
        results = new ArrayList<>();

        //注册
        if (!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }

        SharedPreferences sp = getSharedPreferences("config",MODE_PRIVATE);
        userId = sp.getString("objectId","0");


        RequestData();
        adapter = new NewMessageAdapter(results);
        lastVp.setLayoutManager(new LinearLayoutManager(this));
        lastVp.setAdapter(adapter);
        adapter.setListener(new NewMessageAdapter.NewMessageInterface() {
            @Override
            public void setImage(List<String> list, RecyclerView rv) {
                imageAdapter = new ImageAdapter(list);
                rv.setAdapter(imageAdapter);
            }
        });



    }

    public void RequestData(){
        String url = "https://leancloud.cn:443/1.1/functions/getStatus";
        RequestLast object = new RequestLast(userId, 0, 0, 1);
        Request request = HTTPUtils.requestPost(object, url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e(TAG, "onResponse: " + result);
                ResponseLast bean = new Gson().fromJson(result, ResponseLast.class);
                results.clear();
                results.addAll(bean.getResult());

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });

    }


    //点击发布动态
    public void camera(View view) {
        Intent intent = new Intent(this, SendDonTaiActivity.class);
        startActivityForResult(intent, 21);
    }


    @Subscribe
    public void onEvent(Boolean bo){

       // System.out.println("哈哈哈接收到了："+string);
        RequestData();
        Toast.makeText(this,"发布成功！",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}